<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class AdminStaffActiveCount extends Model
{
    //

    public static function saveLog($staff_id, $school_id, $sex)
    {
        $dormitory_id_list = AdminStaffIcCard::where([
            'staff_id' => $staff_id,
            'school_id' => $school_id,
            'is_delete' => 0
        ])->group('dormitory_id')->column('dormitory_id');
        foreach ($dormitory_id_list as $dormitory_id) {
            $day = date('Y-m-d');
            $hour_index = floor(date('G') / 2);
            $model = AdminStaffActiveCount::where([
                'school_id' => $school_id,
                'dormitory_id' => $dormitory_id,
                'sex' => $sex,
                'day' => $day,
                'hour_index' => $hour_index
            ])->find();
            if (!$model) {
                $model = new AdminStaffActiveCount();
                $model->school_id = $school_id;
                $model->dormitory_id = $dormitory_id;
                $model->sex = $sex;
                $model->day = $day;
                $model->hour_index = $hour_index;
            }
            $model->count = $model->count + 1;
            $model->save();
        }
    }
}
